package com.example.aircraft;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class MyApplication extends Application {
    private static boolean MusicOn;
    private static Context context;
    private static Socket socket;
    private static PrintWriter writer;
    private static BufferedReader in;
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        MusicOn = true;
        socket = new Socket();
        writer = null;
        in = null;
    }

    public static Context getContext() {
        return context;

    }
    public static void setMusicOn(){
        MusicOn = true;
    }
    public static void setMusicOff(){
        MusicOn  = false;
    }
    public static boolean isMusicOn(){
        return MusicOn;
    }
    protected static class NetConnP1 extends Thread{
        @Override
        public void run(){
            try{
                socket = new Socket();
                //运行时修改成服务器的IP
                socket.connect(new InetSocketAddress
                        ("192.168.137.1",1919),5000);
                in = new BufferedReader( new InputStreamReader(socket.getInputStream()));
                writer = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(
                                socket.getOutputStream(),"UTF-8")),true);
                Log.i("client","远程主机地址：" + socket.getRemoteSocketAddress());
            }catch(UnknownHostException ex){
                ex.printStackTrace();
            }catch(IOException ex){
                ex.printStackTrace();
            }
        }
    }
    public static void startSocketListener(){
        new Thread(new NetConnP1()).start();
    }
    public static Socket getSocket(){
        return socket;
    }
    public static PrintWriter getWriter(){
        return writer;
    }
    public static BufferedReader getIn(){
        return in;
    }

}
